<template>
    <el-dialog title="修改密码" v-model="visible" width="25%" destroy-on-close @closed="$emit('closed')">
        <el-container>
            <el-main class="nopadding">
                <el-form :model="form" :rules="rules" ref="dialogForm" label-width="80px">
					<el-input v-model="form.userid" type="hidden"></el-input>
                    <el-form-item label="新密码" prop="pwd">
                        <el-input v-model="form.pwd"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="pwd2">
                        <el-input v-model="form.pwd2"></el-input>
                    </el-form-item>
                </el-form>
            </el-main>
        </el-container>
        <template #footer>
            <el-button @click="visible=false" >取 消</el-button>
            <el-button type="primary" :loading="saveLoading" @click="submit()">保 存</el-button>
        </template>
    </el-dialog>
</template>

<script>
    export default {
        emits: ['success', 'closed'],
        data() {
            return {
                visible: false,
				saveLoading: false,
                //表单数据
                form: {
                    userid:"",
                    pwd: "",
                    pwd2: "",
                },
                //验证规则
                rules: {
					pwd: [
						{required: true, message: '请输入新密码'}
					],
					pwd2: [
						{required: true, message: '请再次输入密码'},
						{validator: (rule, value, callback) => {
							if (value !== this.form.pwd) {
								callback(new Error('两次输入密码不一致!'));
							}else{
								callback();
							}
						}}
					],
				}
            }
        },
		created() {
		},
        methods: {
            open(){
                this.visible = true
                return this
            },
			handleNodeClick(data, checked) {
			},
            //表单提交方法
            async submit() {
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.saveLoading = true;
						var res = await this.$API.system.user.pwd.post(this.form);
						this.saveLoading = false;
						if(res.code === 2000){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success(res.message)
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false;
					}
				});
            },
			//表单注入数据
			setData(data){
				this.form.userid = data.id
			}
        }
    }
</script>
<style scoped>
</style>
